{
  SDL surface wrapper and manager

  Adds support for compile-time surface definitions.
}

Requires Linked-lists
Requires Kagi

struct
  cell+ \ link
  cell+ \ cell field ->w
  cell+ \ cell field ->h
  cell field ->s
end-struct /surface


variable surfaces

: surface'-recreate ( surface' -- )
  dup cell+ @+ swap @ ?dup if
    SDL_CreateRGBSurface( SDL_SWSURFACE -rot 32 $ff0000 $ff00 $ff $ff000000 )
    swap ->s !
  else 2drop then ;

\ enable other compiler extensions to use managed surfaces
: surface!  ->s ! ;
: surface@  ->s @ ;

: surface,   ( surface -- )
  here >r 0 , 0 , 0 , , r> dup surfaces  add-link ;

: blank-surface,   ( w h -- )
  here >r 0 , swap , , 0 , r@ dup surfaces  add-link r> surface'-recreate ;


: Surface:   ( w h -- <name> )
  create blank-surface,
  does> ->s @ ;


\ internal
: surface'-?free
  dup ->s @ ?dup if
    SDL_FreeSurface  ->s off
  else drop then ;

: seki-sdl-cleanup-surfaces
  surfaces follow:[ surface'-?free ;] ;

: seki-sdl-recreate-surfaces
  surfaces follow:[ surface'-recreate ;] ;
